package cn.stylefeng.guns.modular.gui.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.gui.entity.Shequ;
import cn.stylefeng.guns.modular.gui.mapper.ShequMapper;
import cn.stylefeng.guns.modular.gui.model.params.ShequParam;
import cn.stylefeng.guns.modular.gui.model.result.ShequResult;
import cn.stylefeng.guns.modular.gui.service.GuigeService;
import  cn.stylefeng.guns.modular.gui.service.ShequService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 小区管理 服务实现类
 * </p>
 *
 * @author Dong
 * @since 2023-06-09
 */
@Service
public class ShequServiceImpl extends ServiceImpl<ShequMapper, Shequ> implements ShequService {
	@Autowired
    private GuigeService guigeService;

    @Override
    public void add(ShequParam param){
        Shequ entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ShequParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ShequParam param){
        Shequ oldEntity = getOldEntity(param);
        Shequ newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ShequResult findBySpec(ShequParam param){
        return null;
    }

    @Override
    public List<ShequResult> findListBySpec(ShequParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ShequParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ShequParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Shequ getOldEntity(ShequParam param) {
        return this.getById(getKey(param));
    }

    private Shequ getEntity(ShequParam param) {
        Shequ entity = new Shequ();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public LayuiPageInfo listGg(Long sid) {
		List<Map<String,Object>> lm = guigeService.listMaps();
		
		
		if(sid == null) {
			
		}else {
			Shequ sq = this.baseMapper.selectById(sid);
			
			String gids = sq.getGids()+"";
			
			if(!"".equals(gids)) {
				String[] gidsArr = gids.split(",");
				for(int ii=0;ii<lm.size();ii++) {
					String lms = lm.get(ii).get("id").toString();
					boolean isContains = Arrays.asList(gidsArr).contains(lms);
					if(isContains) {
						lm.get(ii).put("selected", true);
					}else {
						lm.get(ii).put("selected", false);
					}
				}
				
			}
			
		}
		
		
		LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(lm);
		return layuiPageInfo;
	}

}
